const app = getApp()
const db = wx.cloud.database();
const collection = "category";
const {
  envList
} = require('../../envList');
Page({
  data: {
    nav2: [],
    active: 0,
    active2: 0,
    cateShowData: [],
    cateBookList: [],
    id: '',
    name: "",
    notData: '',
  },

  getInfo() {
    if (!this.data.nav2[this.data.active]?.node[0]?.url) return;
    let id = this.data.nav2[this.data.active].id;
    db.collection(collection).where({
      type: "secNav",
      "data.id": id
    }).get().then(res => {
      if (res.data?.length) {
        this.setData({
          cateShowData: res.data[0].data.list
        });
        let cateId = (this.data.cateShowData[this.data.active2]?.url).split('?')[1].slice(6);
        console.log(cateId, "id");
        this.getTwoInfo(cateId);
      } else {
        wx.request({
          url: `https://apis.netstart.cn/yunyuedu${this.data.nav2[this.data.active]?.node[0]?.url}`,
          method: 'GET',
          success: (res) => {
            console.log(res.data.list[0].labels)
            this.setData({
              cateShowData: res.data.list[0].labels
            });
            wx.cloud.callFunction({
              name: "addCategory",
              data: {
                data: {
                  id,
                  list: this.data.cateShowData
                },
                type: "secNav"
              }
            });
            let cateId = (this.data.cateShowData[this.data.active2]?.url).split('?')[1].slice(6)
            this.getTwoInfo(cateId);
          }
        })
      }
    });
  },

  getTwoInfo(cateId) {
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: `https://apis.netstart.cn/yunyuedu/source/v2/searchBook.json?catId=${cateId}`,
      method: 'GET',
      success: (res) => {
        this.setData({
          cateBookList: res?.data?.list || []
        })
        wx.hideLoading()
        if (this.data.cateBookList) {
          this.setData({
            notData: '没有找到你想要的内容'
          })
        }
      },
      fail: (res) => {
        console.log(res, 'fail');
        wx.hideLoading()
      }
    })
  },

  // 切换分类导航点击
  changeActive(e) {
    if (e.target.dataset.index == this.data.active) {
      return
    }
    if (e.target.dataset.index == 3) {
      wx.showToast({
        icon: 'none',
        title: '敬请期待',
      })
      return
    }
    this.setData({
      active: e.target.dataset.index,
      active2: 0
    })
    this.getInfo()
  },

  changeActive2(e) {
    if (e.target.dataset.index == this.data.active2) {
      return
    }
    this.setData({
      active2: e.target.dataset.index
    })
    let cateId = (this.data.cateShowData[this.data.active2]?.url).split('?')[1].slice(6)
    this.getTwoInfo(cateId)
  },

  goToSearch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  toDetail(e) {
    wx.navigateTo({
      url: `/pages/detail/detail?id=${e.currentTarget.dataset.id}&title=${e.currentTarget.dataset.title}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    db.collection(collection).where({
      type: 'cateNav'
    }).get().then((res) => {
      console.log(res);
      let list = res.data[0]?.nav2;
      if (!res?.data?.length) {
        wx.request({
          url: 'https://apis.netstart.cn/yunyuedu/store/node.json?gender=1',
          success: (res) => {
            this.setData({
              nav2: res.data.books
            });
            wx.cloud.callFunction({
              name: 'addCategory',
              data: {
                data: res.data.books,
                type: "cateNav"
              }
            });
          }
        });
      }
      this.setData({
        nav2: list
      });
      this.getInfo()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})